package com.fs.dao;

import com.fs.domain.MonitorCatLongSql;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.SelectProvider;
import org.apache.ibatis.jdbc.SqlBuilder;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Map;

/**
 * Created by fengshuai on 17/5/19.
 */
@Repository
public interface MonitorCatLongSqlDao {
    public static final String TABLE = "monitor_cat_long_sql";
    public static final String SELECT_FIELDS = "id,`day`,hour,long_sql,ctime,utime,valid";

    @Select("select " + SELECT_FIELDS + " from " + TABLE + " where valid=1 and `day`=#{day}")
    public List<MonitorCatLongSql> batchGetByDay(@Param("day") int day);

    @SelectProvider(type=SQLProvider.class,method="batchGetByDynamicParam")
    public List<MonitorCatLongSql> batchGetByDynamicParam(@Param("day") int day, @Param("hour") int hour);

    static class SQLProvider{
        public String batchGetByDynamicParam(Map<String, Object> params) {
            SqlBuilder.BEGIN();
            SqlBuilder.SELECT(SELECT_FIELDS);
            SqlBuilder.FROM(TABLE);
            SqlBuilder.WHERE("valid=1");
            SqlBuilder.WHERE("day=#{day}");
            SqlBuilder.ORDER_BY("ctime asc");
            return SqlBuilder.SQL();
        }
    }
}